package com.uit.project.bustrip;

import com.uit.project.bustrip.appconfig.AppsConfig;
import com.uit.project.bustrip.bo.CustomerBOImpl;
import com.uit.project.bustrip.bointerface.CustomerBO;
import com.uit.project.bustrip.entity.Customer;
import com.uit.project.bustrip.valueobject.JSONResult;

import android.os.Bundle;
import android.os.StrictMode;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class Register extends Activity {
	private EditText name;
	private EditText pass;
	private EditText phone;
	private EditText email;
	private EditText address;
	private Button btnRegister;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_register);
		initView();
	}

	private void initView(){
		name = (EditText) findViewById(R.id.customername);
		pass = (EditText) findViewById(R.id.customerpass);
		phone = (EditText) findViewById(R.id.customerphone);
		email = (EditText) findViewById(R.id.customeremail);
		address = (EditText) findViewById(R.id.customeraddress);
		btnRegister = (Button) findViewById(R.id.btnRegister);
		btnRegister.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
				.permitAll().build();
				StrictMode.setThreadPolicy(policy);
				CustomerBO customerBO = new CustomerBOImpl(getApplicationContext());
				Customer customer = getEntityData();
				JSONResult jsonResult = customerBO.registerAsync(customer);
				afterRegister(jsonResult);
			}
		});
	}
	private void afterRegister(JSONResult jsonResult){
		if(jsonResult == null){
			Toast.makeText(getApplicationContext(), AppsConfig.MESSAGE_CONNECTION_ERROR, Toast.LENGTH_SHORT).show();
			return;
		}
		if(jsonResult.getResult().equals(AppsConfig.RESULT_SUCCESS)){
//			Toast.makeText(getApplicationContext(), jsonResult.getMessage(), Toast.LENGTH_SHORT).show();
			Intent i = new Intent(getApplicationContext(),ActiveCustomer.class);
			i.putExtra("code", jsonResult.getData().toString());
			i.putExtra("phone", phone.getText().toString());
			startActivity(i);
		}
		else{
			Toast.makeText(getApplicationContext(), jsonResult.getMessage(), Toast.LENGTH_SHORT).show();
		}
	}
	private Customer getEntityData(){
		Customer customer = new Customer();
		customer.username = name.getText().toString();
		customer.password = pass.getText().toString();
		customer.numberphone = phone.getText().toString();
		customer.email = email.getText().toString();
		customer.address = address.getText().toString();
		return customer;
	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.register, menu);
		return true;
	}

}
